<template>
  <div id="app">
    <h1>{{ count }}</h1>
    <button @click="handleClick">+1</button>

    <hr>

    <input type="text" placeholder="请输入添加的数据" v-model="value" @keydown.enter="handleAdd">
    <ul>
      <li v-for="v in list" :key="v.id">
        <h3>{{ v.title }}</h3>
        <button @click="handleDelete(v.id)">删除</button>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: 'App',
  computed: {
    count() {
      // 获取VueX state 的数据
      return this.$store.state.count
    },
    list() {
      return this.$store.state.list
    }
  },
  data() {
    return {
      value: ''
    }
  },
  methods: {
    handleClick() {
      // 提交 mutations ADD_COUNT
      this.$store.commit('ADD_COUNT', 10)

      // this.$store.commit({
      //   type: 'ADD_COUNT',
      //   num: 10
      // })
    },
    handleDelete(id) {
      this.$store.commit('DELETE_LIST', id)
    },
    handleAdd() {
      this.$store.commit('ADD_LIST', {
        id: new Date().getTime(),
        title: this.value
      })
    }
  }
}
</script>